package org.dromara.mall.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.mall.domain.MallGoods;
import org.dromara.mall.domain.bo.GoodsBo;
import org.dromara.mall.domain.vo.GoodsVo;
import org.dromara.mall.service.IMallGoodsService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品列表
 *
 * @author Lion Li
 * @date 2024-05-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mall/mallGoods")
public class MallGoodsController extends BaseController {

    private final IMallGoodsService mallGoodsService;

    /**
     * 查询商品列表列表
     */
    @SaCheckPermission("mall:mallGoods:list")
    @GetMapping("/list")
    public TableDataInfo<GoodsVo> list(GoodsBo bo, PageQuery pageQuery) {
        return mallGoodsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出商品列表列表
     */
    @SaCheckPermission("mall:mallGoods:export")
    @Log(title = "商品列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(GoodsBo bo, HttpServletResponse response) {
        List<GoodsVo> list = mallGoodsService.queryList(bo);
        ExcelUtil.exportExcel(list, "商品列表", GoodsVo.class, response);
    }

    /**
     * 获取商品列表详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("mall:mallGoods:query")
    @GetMapping("/{id}")
    public R<GoodsVo> getInfo(@NotNull(message = "主键不能为空")
                                  @PathVariable Long id) {
        return R.ok(mallGoodsService.queryById(id));
    }

    /**
     * 新增商品，这是自定义写法，废弃
     */

    @SaCheckPermission("mall:mallGoods:add")
    @Log(title = "商品新增", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody GoodsBo bo) {
        MallGoods goods = mallGoodsService.insertByBo(bo);
        //返回生成的id
        return R.ok(goods.getId());
    }

    /**
     * 修改商品
     */
    @SaCheckPermission("mall:mallGoods:edit")
    @Log(title = "商品列表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Long> edit(@Validated(EditGroup.class) @RequestBody GoodsBo bo) {
        MallGoods goods =mallGoodsService.updateByBo(bo);
        return R.ok(goods.getId());
    }

    /**
     * 删除商品列表
     *
     * @param ids 主键串
     */
    @SaCheckPermission("mall:mallGoods:remove")
    @Log(title = "商品列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(mallGoodsService.deleteWithValidByIds(List.of(ids), true));
    }

    /*测试定时任务*/
    //每隔5秒执行一次方法
//    @Scheduled(cron = "0/5 * * * * ?")
//    public void testGoods() {
//        System.out.println("每隔5秒执行一次");
//    }

    /*商品审核*/
    @SaCheckPermission("mall:mallGoods:verify")
    @Log(title = "商品审核", businessType = BusinessType.VERIFY)
    @PutMapping("/updateVerify/{id}/{goodsVerify}")
    public R<Void> verify(@NotNull(message = "主键不能为空")
                          @PathVariable Long id,
                          @PathVariable Integer goodsVerify
    ) {
        return toAjax(mallGoodsService.updateVerify(id, goodsVerify, true));
    }

    /*商品上下架*/
    @SaCheckPermission("mall:mallGoods:status")
    @Log(title = "商品审核", businessType = BusinessType.STATUS)
    @PutMapping("/updateStatus/{id}/{goodsStatus}")
    public R<Void> updateStatus(@NotNull(message = "主键不能为空")
                                @PathVariable Long id,
                                @PathVariable Integer goodsStatus
    ) {
        return toAjax(mallGoodsService.updateStatus(id, goodsStatus, true));
    }
    /*推荐商家首页*/
    @SaCheckPermission("mall:mallGoods:isRecommend")
    @Log(title = "推荐商家", businessType = BusinessType.STATUS)
    @PutMapping("/updateRecommend/{id}/{isRecommend}")
    public R<Void> updateRecommend(@NotNull(message = "主键不能为空")
                                @PathVariable Long id,
                                @PathVariable Integer isRecommend
    ) {
        return toAjax(mallGoodsService.updateRecommend(id, isRecommend, true));
    }
    /*推荐主站首页*/
    @SaCheckPermission("mall:mallGoods:isRecommend")
    @Log(title = "推荐主站", businessType = BusinessType.STATUS)
    @PutMapping("/updateMaster/{id}/{isMaster}")
    public R<Void> updateMaster(@NotNull(message = "主键不能为空")
                                @PathVariable Long id,
                                @PathVariable Integer isMaster
    ) {
        return toAjax(mallGoodsService.updateMaster(id, isMaster, true));
    }

    /**
     * 删除商品sku列表
     *
     * @param ids 主键串
     */
    @SaCheckPermission("mall:mallGoods:remove")
    @Log(title = "删除SKU列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/skus/{ids}")
    public R<Void> delSku(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(mallGoodsService.deleteSkuByGoodsId(List.of(ids), true));
    }

}
